package com.vivo.tool.eclipsestarter;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JViewport;
import javax.swing.WindowConstants;

/**
 * This code was generated using CloudGarden's Jigloo
 * SWT/Swing GUI Builder, which is free for non-commercial
 * use. If Jigloo is being used commercially (ie, by a corporation,
 * company or business for any purpose whatever) then you
 * should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details.
 * Use of Jigloo implies acceptance of these licensing terms.
 * *************************************
 * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED
 * for this machine, so Jigloo or this code cannot be used legally
 * for any corporate or commercial purpose.
 * *************************************
 */
/**
 * 
 * @author vivo
 * @version 0.13
 * 
 */
public class CenterPanelBean extends javax.swing.JPanel {

	Font font = new Font("Tahoma", Font.BOLD, 11);
	// start GUI -->
	private JButton jbStartEclipse;
	private JTextField jtfPluginPath;
	private JLabel jlblPluginPath;
	private JScrollPane jspPluginList;
	private JCheckBox jcbSavePluginSuit;
	private JPanel jpTop;
	private JPanel jpOptions;
	private JButton jbls;
	private JButton jbGetPluginList;
	private JCheckBox jcbReGenerateLinkFile;

	public CenterPanelBean() {
		super();
		initGUI();
	}

	private void initGUI() {
		try {

			this.setPreferredSize(new java.awt.Dimension(612, 372));
			BorderLayout thisLayout = new BorderLayout();
			thisLayout.setHgap(5);
			thisLayout.setVgap(2);
			this.setLayout(thisLayout);
			{
				jspPluginList = new JScrollPane();
				this.add(jspPluginList, BorderLayout.CENTER);
			}
			{
				jpOptions = new JPanel();
				FlowLayout jpOptionsLayout = new FlowLayout();
				jpOptionsLayout.setAlignment(FlowLayout.LEFT);
				jpOptions.setLayout(jpOptionsLayout);
				this.add(jpOptions, BorderLayout.WEST);
				jpOptions.setBounds(11, 89, 272, 248);
				jpOptions.setName("optSetPluginPath");
				jpOptions.setPreferredSize(new java.awt.Dimension(273, 316));
				{
					jcbReGenerateLinkFile = new JCheckBox();
					jpOptions.add(jcbReGenerateLinkFile);
					jcbReGenerateLinkFile.setText("ReGenerate \"Links\" Directory (Delete first).");
					jcbReGenerateLinkFile.setBounds(9, 80, 273, 19);
					jcbReGenerateLinkFile.setPreferredSize(new java.awt.Dimension(269, 15));
					jcbReGenerateLinkFile.setName("optReGenerate");
				}
				{
					jcbSavePluginSuit = new JCheckBox();
					jpOptions.add(jcbSavePluginSuit);
					jcbSavePluginSuit.setText("Save my choice");
					jcbSavePluginSuit.setPreferredSize(new java.awt.Dimension(114, 18));
					jcbSavePluginSuit.setName("optSavePluginSuit");
					jcbSavePluginSuit.setSelected(true);
				}

			}
			{
				jpTop = new JPanel();
				this.add(jpTop, BorderLayout.NORTH);
				jpTop.setLayout(null);
				jpTop.setPreferredSize(new java.awt.Dimension(630, 74));
				{
					jlblPluginPath = new JLabel();
					jpTop.add(jlblPluginPath);
					jlblPluginPath.setText("Please input the Plugin's Path:");
					jlblPluginPath.setBounds(8, 5, 185, 16);
				}
				{
					jbls = new JButton();
					jpTop.add(jbls);
					jbls.setText("jbls");
					jbls.setBounds(277, 12, 116, 20);
					jbls.setFont(new java.awt.Font("Verdana", 0, 9));
					jbls.addMouseListener(new MouseAdapter() {

						public void mouseClicked(MouseEvent evt) {
							jblsMouseClicked(evt);
						}
					});
				}
				{
					jtfPluginPath = new JTextField();
					jpTop.add(jtfPluginPath);
					jtfPluginPath.setText("E:\\java\\Eclipse-Plugin");
					jtfPluginPath.setBounds(33, 24, 210, 18);
				}
				{
					jbGetPluginList = new JButton();
					jpTop.add(jbGetPluginList);
					jbGetPluginList.setText("\u83b7\u53d6\u5217\u8868");
					jbGetPluginList.setBounds(278, 47, 145, 21);
					jbGetPluginList.addMouseListener(new MouseAdapter() {

						public void mouseClicked(MouseEvent evt) {
							jbGetPluginListMouseClicked(evt);
						}
					});
				}
				{
					jbStartEclipse = new JButton();
					jpTop.add(jbStartEclipse);
					jbStartEclipse.setText("StartEclipse");
					jbStartEclipse.setBounds(486, 46, 120, 22);
					jbStartEclipse.addMouseListener(new MouseAdapter() {

						public void mouseClicked(MouseEvent evt) {
							jbStartEclipseMouseClicked(evt);
						}
					});
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// end GUI ---<

	private static final long serialVersionUID = 1L;

	/**
	 * Auto-generated main method to display this JPanel inside a new JFrame.
	 */
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.getContentPane().add(new CenterPanelBean());
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}

	boolean isjcbReGenerateLinkFileSelected() {
		return jcbReGenerateLinkFile.isSelected();
	}

	/**
	 * StartEclipse
	 */
	private void jbStartEclipseMouseClicked(MouseEvent evt) {

		// get selected options in jpOptions
		ArrayList alOptions = getSelectedOptions();
		// do selected action First.
		LinkFileManager.doOptions(alOptions);

		// start Eclipse.
		try {
			Process P = Runtime.getRuntime().exec("notepad.exe");
		} catch (IOException e) {
			e.printStackTrace();
		}

		// Exit Starter.
		System.exit(0);

	}

	/**
	 * get selected options in jpOptions,they are:<BR>
	 * <ul>
	 * <li> optReGenerate </li>
	 * <li> optSetPluginPath </li>
	 * <li> optSavePluginSuit </li>
	 * <ul>
	 */
	private ArrayList getSelectedOptions() {

		// store option actions
		ArrayList<String> alStartOptions = new ArrayList<String>();

		// add default option, set Constants.strpluginpath.
		alStartOptions.add("optSetPluginPath");

		// traverse all the JCheckBox in jpOptions Jpanel.
		Object[] components = jpOptions.getComponents();

		for (Object component : components) {
			if (component instanceof JCheckBox) {
				if (((JCheckBox) component).isSelected()) {
					Tool.assertPrintln(((JCheckBox) component).getName() + "  selected");
					alStartOptions.add(((JCheckBox) component).getName());
				}
			}
		}
		return alStartOptions;
	}

	/**
	 * get selected plugins.
	 * 
	 * @return
	 */
	public ArrayList getSelectedPlugins() {
		ArrayList alSelectedPlugins = new ArrayList();
		if (jpPluginList == null) {
			return null;
		}
		Object[] objs = jpPluginList.getComponents();
		for (Object obj : objs) {
			if (obj instanceof JCheckBox && ((JCheckBox) obj).isSelected()) {
				alSelectedPlugins.add(((JCheckBox) obj).getText());
			}
		}
		return alSelectedPlugins;
	}

	/**
	 * get textfield's content.
	 * 
	 * @return
	 */
	public String getPluginPathName() {
		return jtfPluginPath.getText();
	}

	// jbls
	private void jblsMouseClicked(MouseEvent evt) {
		// TODO add your code for jbls.mouseClicked
		LinkFileManager.replaceEclipseLinkFiles();
	}

	// Plugins container.
	JPanel jpPluginList = null;

	/**
	 * GetPlugin list
	 * 
	 * @param evt
	 */
	private void jbGetPluginListMouseClicked(MouseEvent evt) {
		// TODO The scrollbar move too slow when turn the wheel.

		Tool.assertPrintln("jbGetPluginList clicked!");

		// get plugin list.
		setPluginListCheckBoxPanel(LinkFileManager.getPluginList(Constant.strPluginPath,
				jcbReGenerateLinkFile.isSelected()));
	}

	/**
	 * Fill jspPluginList with plugin checkboxs.Data from a ArrayList.
	 * <ul>
	 * <LI>First, delete all the objects in it.</li>
	 * <LI>Then, fill it with checkbox list from a Arraylist.</li>
	 * <LI>The last,read it from default suit file.</li>
	 * <ul>
	 */
	public void setPluginListCheckBoxPanel(ArrayList alPluginList) {

		// First, delete all the objects in jspPluginList.
		delPluginList();

		// if empty, exit.
		if (alPluginList == null || alPluginList.isEmpty())
			return;

		// Then, fill it with checkbox list from a A rraylist.
		// JPanel
		jpPluginList = new JPanel();
		jpPluginList.setVisible(false);
		BoxLayout jspBoxLayout = new BoxLayout(jpPluginList, BoxLayout.Y_AXIS);
		jpPluginList.setLayout(jspBoxLayout);

		// viewport
		JViewport jvpPluginList = jspPluginList.getViewport();
		jvpPluginList.add(jpPluginList);

		// JCheckBoxs
		int i = 0;
		String strLinkFileName;
		JCheckBox[] jcbLinkFiles = new JCheckBox[alPluginList.size()];

		for (Object al : alPluginList) {
			strLinkFileName = (String) al;
			jcbLinkFiles[i] = new JCheckBox();
			jcbLinkFiles[i].setText(strLinkFileName);
			jcbLinkFiles[i].setFont(font);
			jcbLinkFiles[i].setPreferredSize(new Dimension(-1, 13));
			jpPluginList.add(jcbLinkFiles[i]);
			// TODO auto adjust width >= some number.
			i++;
			if (i > alPluginList.size()) {
				Tool.println("Error ! out of bound.");
			}
		}
		jpPluginList.setVisible(true);

		// The last,read it from default suit file.
	}

	/**
	 * Delete everything in jspPluginList.
	 */
	public void delPluginList() {

		if (jpPluginList == null)
			return;
		jpPluginList.setVisible(false);

		Object[] objs = jpPluginList.getComponents();
		if (objs.length == 0) {
			Tool.println("has nothing");
			return;
		}

		for (Object obj : objs) {
			obj = null;
		}
		jpPluginList = null;

	}

	public void restorePluginSuit(ArrayList alPluginSuit) {
		// if null or empty, return,
		if (alPluginSuit == null || alPluginSuit.isEmpty()) {
			Tool.println("Empty, exit");
			return;
		}
		// select them.
		loadPluginSuit();
	}

	/**
	 * Load Plugin selected suit form presaved file.
	 * 
	 */
	public void loadPluginSuit() {
		ArrayList al = Tool.readFileToArrayList(Constant.strMySelection);
		// 
		if (jpPluginList == null)
			return;

		Object[] objs = jpPluginList.getComponents();
		if (objs.length == 0) {
			Tool.println("has nothing");
			return;
		}
		for (Object obj : objs) {
			if (obj instanceof JCheckBox) {
				JCheckBox jcb = (JCheckBox) obj;
				if (al.contains(jcb.getText())) {
					jcb.setSelected(true);
					jcb.setFont(new Font("Tahoma", Font.PLAIN, 11));
				} else {
					jcb.setSelected(false);
					jcb.setFont(new Font("Tahoma", Font.PLAIN, 11));
				}
			}
		}
	}
}
